package com.javajun.spider.bt7086.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.javajun.spider.bt7086.entity.Video;
import com.javajun.spider.bt7086.service.VideoService;

@Controller
public class IndexController {
	
	@Autowired
	private VideoService service;

	@GetMapping
	@ResponseBody
	public Object index(ModelMap map,
			@RequestParam(name = "pageNum", defaultValue = "1") int pageNum,
			@RequestParam(name = "size", defaultValue = "10") int size) {
		
		PageRequest request = PageRequest.of(--pageNum, size);
		Page<Video> page = service.findAll(request);
		
		List<Video> contentList = page.getContent(); //内容
		map.put("contentList", contentList);

		int totalPages = page.getTotalPages(); //总页数
		map.put("totalPages", totalPages);
		
		int nowPageNum = page.getNumber() + 1; //当前页
		map.put("nowPageNum", nowPageNum);
		
		boolean hasNext = page.hasNext();
		map.put("hasNext", hasNext); //是否还有上一页
		
		boolean hasPrevious = page.hasPrevious();
		map.put("hasPrevious", hasPrevious); //是否还有下一页
		
		return "index";
	}
}
